//
//  NhacNgoaiViewController.m
//  NhomThuy02
//
//  Created by Nguyen Thien Chuong on 2/3/15.
//  Copyright (c) 2015 LamQuoc. All rights reserved.
//

#import "NhacNgoaiViewController.h"
#import "NhacNgoaiTableViewCell.h"
#import "SongDetailViewController.h"
@interface NhacNgoaiViewController ()<UITableViewDelegate, UITableViewDataSource,UISearchControllerDelegate,UISearchDisplayDelegate>
@property (nonatomic,strong)NSArray *searchArray;
@property (nonatomic,strong)NSArray *songList;
@property (nonatomic,strong)NSString *searchString;
@end

@implementation NhacNgoaiViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad


{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.nhacNgoaiTableView.delegate = self;
    self.nhacNgoaiTableView.dataSource =self;
    UILongPressGestureRecognizer* _longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self.overlay action:@selector(longPressDetected:)];
    [self.btnMenu addGestureRecognizer:_longPressRecognizer];
    self.searchDisplayController.searchResultsDataSource= self;
    self.searchDisplayController.searchResultsDelegate = self;
    self.songList =[ManagerData defaultManager].enSongList ;
    self.searchString = @"";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* cellID = @"nhacngoai_cell_id";
    NhacNgoaiTableViewCell* cell = [self.nhacNgoaiTableView dequeueReusableCellWithIdentifier:cellID];
    NSDictionary* detal;
    if (![self.searchString isEqualToString:@""]) {
        detal = [self.searchArray objectAtIndex:indexPath.row];
    }else{
        detal = [self.songList objectAtIndex:indexPath.row];
    }
    
    cell.tenBH.text = [[detal objectForKey:@"song-name"]capitalizedString];
    cell.loiBH.text = [detal objectForKey:@"lyrics"];
    cell.maSoBH.text = [NSString stringWithFormat:@"%@", [detal objectForKey:@"song-code"]];
    [cell.maSoBH setBackgroundColor:[UIColor greenLightColor]];
    return cell;
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%@",self.searchDisplayController.searchBar.text);
    if (![self.searchString isEqualToString:@""]) {
        return self.searchArray.count;
    }
    return self.songList.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 82;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath* indexPath;
    
    NSDictionary* dictionary = self.songList[indexPath.row];
    if (![self.searchString isEqualToString:@""]) {
        indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
        dictionary = [self.searchArray objectAtIndex:indexPath.row];
    }else{
        indexPath = [self.nhacNgoaiTableView indexPathForSelectedRow];
        dictionary = [self.songList objectAtIndex:indexPath.row];
    }
    ((SongDetailViewController*)segue.destinationViewController).songData = dictionary;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    NSPredicate *pre2 = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        NSDictionary *song =evaluatedObject;
        NSString *name =[song objectForKey:@"song-name"];
        if([[name lowercaseString] containsString:[searchString lowercaseString]]) {
            return YES;
        } else {
            return NO;
        }
    }];
    self.searchArray = [self.songList filteredArrayUsingPredicate:pre2];
    [self.searchDisplayController.searchResultsTableView reloadData];
    self.searchString= searchString;
    return YES;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{
    self.searchString = @"";
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
